package com.app.Activity;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.util.ArrayList;
import java.util.Arrays;

import org.achartengine.chart.BarChart;


import com.app.Controller.ActivityController;
import com.app.Controller.NetworkController;
import com.app.Controller.ReadWriteFileController;
import com.app.DTO.AccountDTO;
import com.app.DTO.RoleDTO;
import com.app.Network.Protocol;
import com.app.PhanQuyen.Quyen;
import com.app.Tree.BoardView;
import com.app.Tree.BoardViewTN;
import com.app.Tree.INode;
import com.app.Tree.TextNode;
import com.app.Tree.TreeNode;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
//--

public class frmTreeNodeActivity extends Activity implements View.OnClickListener {
	/** Called when the activity is first created. */
	public static Runnable raAddnode = null;
	public static Runnable raAddnodetext = null;
	public static Runnable raUpdateTextNode = null;
	public static Runnable raUpdateNodeText = null;
	public static Runnable raDeleteNode = null;
	public static Runnable raDeleteNodeText = null;
	public static Runnable raResetTree = null;
	LinearLayout layout;
	String value = "";
	public static BoardView board = null;
	private BoardViewTN boardTN;
	LayoutInflater layoutInflater;
	FrameLayout frmLayout;
	Button btnSwitch, btnSaveTree, btnDeleteAll;
	public static long idNode;
	public static String textUpdate = "";
	public static long id = 0;
	public static TreeNode nodeAdd;
	public static TextNode textnodeAdd;
	public static boolean isActive=false;
	public static int flag;
	private boolean sw=false;
	Button btnZoomOut, btnZoomIn;
	float scale=1.0f;
	public static long version;
	public static int lb;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_tree_node);
		
		Log.d("Phu", "frmTreeNodeActivity onCreate");
		isActive=true;
		board = (BoardView) findViewById(R.id.boardID);
		boardTN=(BoardViewTN)findViewById(R.id.boardtextID);
		boardTN.setVisibility(View.GONE);
		board.setVisibility(View.VISIBLE);
//		board.readXml(BoardView.treePath);
//		Log.d("Phu", "frmTreeNodeActivity-... đọc file vao nodes :.." + board.getTree().getSumChildren());
        btnZoomIn=(Button)findViewById(R.id.btnZoomIn);
        btnZoomOut=(Button)findViewById(R.id.btnZoomOut);
        btnZoomIn.setOnClickListener(this);
        btnZoomOut.setOnClickListener(this);

		
		

		
		board.isActive=true;		
//		boardTN.isActive = false;
		
		raAddnode = new Runnable() {
			@Override
			public void run() {	
				Log.d("Phu", "frmTreeNodeActivity-raAddnode....board.isActive= " + board.isActive);
				if(board.isActive){
					Log.d("Phu", "da vao raAddnode roi" + (board==null));
					board.addNode(idNode, nodeAdd, BoardView.treePath);
					board.autoArrange();
				}
				
			}
		};
		raAddnodetext = new Runnable() {
			@Override
			public void run() {	
				if(boardTN.isActive){
					Log.d("Phu", "frmTreeNodeActivity-raAddnodetext ,kakaka" + (boardTN==null));
					boardTN.addNode(idNode, textnodeAdd, BoardViewTN.treePath);
				}
				boardTN.autoArrange();
			}
		};
		raUpdateTextNode = new Runnable() {
			@Override
			public void run() {
				if(board.isActive){
					board.updateText(nodeAdd, BoardView.treePath);
					
				}
			}
		};
		raUpdateNodeText = new Runnable() {
			@Override
			public void run() {
				if(boardTN.isActive)
					boardTN.updateText(textnodeAdd, BoardViewTN.treePath);
			}
		};
		raDeleteNode = new Runnable() {

			@Override
			public void run() {
				Log.d("An", "frmTreeNodeActivity-raDeleteNode... board.isActive= "+ board.isActive);
				if(board.isActive){
					Log.d("An", "frmTreeNodeActivity-raDeleteNode... id= "+nodeAdd.getId() );		
					board.removeNodeById(nodeAdd, BoardView.treePath);
				}
			}
		};
		raDeleteNodeText = new Runnable() {

			@Override
			public void run() {				
				if(boardTN.isActive){
					Log.d("An", "frmTreeNodeActivity-raDeleteNode Text: idNode= " + nodeAdd.getId());		
					boardTN.removeNodeById(textnodeAdd, BoardViewTN.treePath,BoardViewTN.DeletedNodesPath);
				}
			}
		};
		raResetTree=new Runnable() {
			
			@Override
			public void run() {
//				resetTree();
			}
		};
		btnSwitch=(Button)findViewById(R.id.btnSwitch);
		btnSwitch.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(boardTN.isActive){
					
					//Ghi cây bên text tree xuống file
					boardTN.isActive = false;
					boardTN.setVisibility(View.GONE);
					boardTN.writeXML(BoardView.treePath);
					
					//Đọc tree node từ file lên
					board.readXml(BoardView.treePath);
					board.autoArrange();
					board.setVisibility(View.VISIBLE);
					board.isActive=true;
					board.moveCenter();
					sw=false;
				}else{
					//Ghi cây bên tree node xuống file
					board.writeXML(BoardView.treePath);
					board.setVisibility(View.GONE);
					board.isActive=false;
					
					//Đọc text tree từ file lên
					boardTN.isActive = true;					
					boardTN.readXml(BoardView.treePath);
					//boardTN.au
					boardTN.setVisibility(View.VISIBLE);
					boardTN.settree(3, 2);
					sw=true;
				}
			}
		});
	}

	public void showAlert() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		final EditText input = new EditText(this);
		alert.setTitle("Nhập nội dung!");
		alert.setView(input);
		alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				value = input.getText().toString();
			}

		});

		alert.show();
	}

	@Override
	public void onClick(View v) {
		int id=v.getId();
		switch (id) {
		case R.id.btnZoomIn:
			scale+=0.2;
			board.zoom(scale);
			break;
		case R.id.btnZoomOut:
//			scale-=0.2;
//			if(scale<=0.2)
//				scale=0.2f;
//			board.zoom(scale);
			NetworkController.sendVersionTree("192.168.1.88:"+Protocol.PORT, board.treeversion());
			break;
		default:
			break;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d("Phu", "frmTreeNodeActivity onResume");
		board.readXml(BoardView.treePath);
		board.autoArrange();
		board.isActive=true;
	}
	@Override
	public void onStop() {
		super.onStop();		
		isActive=false;
		board.writeXML(BoardView.treePath);
		Log.d("Phu", "frmTreeNodeActivity onStop");
	}

	@Override
	public void onPause() {
		super.onPause();
		isActive=false;
		board.writeXML(BoardView.treePath);
		board.isActive = false;
		Log.d("Phu", "frmTreeNodeActivity onPause");
	}
@Override
public void onDestroy(){
	super.onDestroy();
	Log.d("Phu", "frmTreeNodeActivity onDestroy");
}
	@Override
	public void onStart() {
		super.onStart();		
		board.isActive = true;
		boardTN.isActive = false;
		isActive=true;
		if (board != null) {
			try {
				
				board.readXml(BoardView.treePath);
				board.autoArrange();
			} catch (Exception e) {
				Log.d("Phu", "ko doc duoc tree:" + e);
			}
		}
		if (NetworkController._peer == null) {
			String s = "";
			AccountDTO account = new AccountDTO();
			account.set_Username("quocphu");
			ArrayList<RoleDTO> roleList = ReadWriteFileController.readRole(null,ReadWriteFileController.MY_ROLE_PATH);

			for (RoleDTO role : roleList) {
				account.addRole(role);
				Log.i("Phu","Role: "+role.getName());
			}
			NetworkController.init("MES", Protocol.PORT, account);	
			
		}
		
		
		this.setTitle(NetworkController._peer.getAddressPeer());
	}

	
}